package com.nantai.jiafang.nantaijiafang.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.fragment.FragMessagePush;
import com.nantai.jiafang.nantaijiafang.fragment.FragSearchData;

public class MainActivity extends FragmentActivity {

    private final static int TAB_MESSAGE_PUSH = 0;
    private final static int TAB_DATE_SEARCH = 1;
    public final static int MESSAGE_PUSHUPDATE = 0x1001;
    public FragMessagePush fragPush;
    public FragSearchData fragSearch;

    private FragmentManager fm;
    private FragmentTransaction ft;
    private Fragment[] mFragments;

    private RelativeLayout mRlPush;
    private RelativeLayout mRlSearch;
    private ImageView mIvPush;
    private ImageView mIvData;
    public static MyHandler myHandler;
    private int current_tab = TAB_MESSAGE_PUSH;


    public class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MESSAGE_PUSHUPDATE:
                    if (fragPush!=null){
                        fragPush.updateNewMessage();
                    }
                    break;
            }
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHandler = new MyHandler();
        init();
        setup();
    }

    private void init() {
        mIvPush = (ImageView) findViewById(R.id.iv_push);
        mIvData = (ImageView) findViewById(R.id.iv_data);
        mRlPush = (RelativeLayout) findViewById(R.id.rl_push);
        mRlSearch = (RelativeLayout) findViewById(R.id.rl_data);

        fm = getSupportFragmentManager();

        fragPush = (FragMessagePush) fm.findFragmentById(R.id.fragPush);
        fragSearch = (FragSearchData) fm.findFragmentById(R.id.fragSearch);

        mFragments = new Fragment[2];
        mFragments[0] = fragPush;
        mFragments[1] = fragSearch;

        ft = fm.beginTransaction().show(mFragments[0]).hide(mFragments[1]);
        ft.commit();
    }

    private void setup() {

        mRlPush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (current_tab == TAB_MESSAGE_PUSH) {

                } else {
                    mIvPush.setImageResource(R.mipmap.img_push_selected);
                    mIvData.setImageResource(R.mipmap.img_data_normal);
                    current_tab = TAB_MESSAGE_PUSH;
                    ft = fm.beginTransaction().show(mFragments[0])
                            .hide(mFragments[1]);
                    ft.commit();
                }
            }
        });

        mRlSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (current_tab == TAB_DATE_SEARCH) {

                } else {
                    mIvPush.setImageResource(R.mipmap.img_push_normal);
                    mIvData.setImageResource(R.mipmap.img_data_selected);
                    current_tab = TAB_DATE_SEARCH;
                    ft = fm.beginTransaction().show(mFragments[1])
                            .hide(mFragments[0]);
                    ft.commit();
                }
            }
        });

    }


}
